<template>
  <div class="category">
    <van-search class="select" placeholder="商品搜索" shape="round" />
    <div class="sideBar">
      <ul>
        <li
          :class="selectIndex == index ? 'active' : ''"
          v-for="(item, index) in tabList"
          :key="item.id"
          @click="tabEvents(index, item.id)"
        >
          {{ item.name }}
        </li>
      </ul>
    </div>
    <div class="content">
      <img :src="childimg" alt="" />
      <h2>{{ childtitle }}</h2>
      <div class="child">
        <div
          class="childitem"
          v-for="item in child"
          :key="item.id"
          @click="goCategory(item.id)"
        >
          <img :src="item.wap_banner_url" alt="" />
          <p>{{ item.name }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { indexaction, currentaction } from "@/api/category/index";
export default {
  name: "Index",

  data() {
    return {
      tabList: [],
      child: [],
      selectIndex: 0,
      childimg: "",
      childtitle: "",
      boxid:''
    };
  },
  async created() {
    var result = await indexaction({});
    this.tabList = result.categoryList;
    currentaction({
      id: this.tabList[this.selectIndex].id,
    }).then((res) => {
      this.child = res.data.currentOne.subList;
      this.childimg = res.data.currentOne.banner_url;
      this.childtitle = res.data.currentOne.name + "分类";
    });
  },
  mounted() {},

  methods: {
    tabEvents(index, id) {
      this.selectIndex = index;
      this.boxid = id
      currentaction({
        id: id,
      }).then((res) => {
        this.child = res.data.currentOne.subList;
        this.childimg = res.data.currentOne.banner_url;
        this.childtitle = res.data.currentOne.name + "分类";
        
      });
    },
    goCategory(val) {
      this.$router.push({
        name: "categoryList",
        query: {
          id: `${val}`,
          boxid:this.boxid
        },
      });
    },
  },
};
</script>

<style scoped lang="scss">
@import "../category/scss/index.scss";
* {
  margin: 0;
  padding: 0;
}
</style>